package valatile;

import java.util.concurrent.ExecutorService;

/**
 * @author Aug28-tlsjn
 * @date 2021-04-11 Sunday 17:10
 */
public class SingletonDemo {

  private static volatile SingletonDemo instance = null;

  private SingletonDemo() {
    System.out.println(Thread.currentThread().getName() + ": Constructor called.");
  }

  public static SingletonDemo getInstance() {
    if (instance == null) {
      synchronized (SingletonDemo.class) {
        if (instance == null) {
          instance = new SingletonDemo();
        }
      }
    }
    return instance;
  }

  public static void main(String[] args) {
    /*
    System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
    System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
    System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
    */
    ExecutorService executorService = ThreadUtils.getExecutorService(5, 8, 2, 3);
    for (int i = 0; i < 10; i++) {
      executorService.execute(() -> {
        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
      });
    }
    executorService.shutdown();
  }
}
